package com.lframework.starter.mybatis.impl.system;

import com.github.pagehelper.PageInfo;
import com.lframework.common.utils.Assert;
import com.lframework.common.utils.StringUtil;
import com.lframework.starter.mybatis.annotations.OpLog;
import com.lframework.starter.mybatis.entity.SysOrg;
import com.lframework.starter.mybatis.enums.OpLogType;
import com.lframework.starter.mybatis.impl.BaseMpServiceImpl;
import com.lframework.starter.mybatis.mappers.system.SysOrgMapper;
import com.lframework.starter.mybatis.resp.PageResult;
import com.lframework.starter.mybatis.service.system.ISysOrgService;
import com.lframework.starter.mybatis.utils.PageHelperUtil;
import com.lframework.starter.mybatis.utils.PageResultUtil;
import com.lframework.starter.mybatis.vo.system.org.QuerySysOrgVo;
import com.lframework.starter.web.utils.JsonUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.io.Serializable;
import java.util.List;

/**
 * laich  Date:2022/9/8
 *
 * @Desciprtion:
 */
@Slf4j
@Service
public class SysOrgServiceImpl extends BaseMpServiceImpl<SysOrgMapper, SysOrg> implements ISysOrgService {
    @Override
    public PageResult<SysOrg> query(Integer pageIndex, Integer pageSize, QuerySysOrgVo vo) {

        Assert.greaterThanZero(pageIndex);
        Assert.greaterThanZero(pageSize);

        PageHelperUtil.startPage(pageIndex, pageSize);
        List<SysOrg> datas = this.query(vo);

        return PageResultUtil.convert(new PageInfo<>(datas));
    }

    @Override
    public List<SysOrg> query(QuerySysOrgVo vo) {

        return getBaseMapper().query(vo);
    }

    @Override
    public SysOrg findById(Integer id) {

        return getBaseMapper().selectById(id);
    }

    @OpLog(type = OpLogType.OTHER, name = "删除组织表，ID：{}", params = {"#id"})
    @Transactional
    @Override
    public void deleteById(Integer id) {
        SysOrg sysOrg = getBaseMapper().selectById(id);
        log.info("删除组织=>", JsonUtil.toJsonString(sysOrg));
        getBaseMapper().deleteById(id);
    }

    @Override
    public boolean saveOrUpdate(QuerySysOrgVo vo) {
        if (StringUtils.isEmpty(vo.getId())) {
            //save
            SysOrg org = new SysOrg();
            BeanUtils.copyProperties(vo, org);
            return getBaseMapper().insert(org) > 0;
        } else {
            return this.lambdaUpdate().set(StringUtil.isNotEmpty(vo.getName()), SysOrg::getName, vo.getName())
                    .set(StringUtil.isNotEmpty(vo.getOrgNo()), SysOrg::getOrgNo, vo.getOrgNo())
                    .set(StringUtil.isNotEmpty(vo.getPhone()), SysOrg::getPhone, vo.getPhone())
                    .set(vo.getAvailable()!=null, SysOrg::getAvailable,vo.getAvailable())
                    .eq(SysOrg::getId, vo.getId()).update();
        }
    }

    @Override
    public void cleanCacheByKey(Serializable key) {

    }


}
